package com.guigu.tms.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.tms.commons.entity.aggregation.CoreOrg;
import com.guigu.tms.commons.order.entity.vo.Result;
import com.guigu.tms.commons.security.entity.AuthUser;
import com.guigu.tms.security.mapper.AuthUserMapper;
import com.guigu.tms.security.mapper.CoreOrgMapper;
import com.guigu.tms.security.service.IAuthUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.tms.security.service.IValidateCodeService;
import com.guigu.tms.security.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2022-10-16
 */
@Service
public class
AuthUserServiceImpl extends ServiceImpl<AuthUserMapper, AuthUser> implements IAuthUserService {

    @Autowired
    private AuthUserMapper userMapper;
    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Autowired
    private IValidateCodeService validateCodeService;

    @Autowired
    private CoreOrgMapper coreOrgMapper;

    private final String key="courier";
    @Override
    public Result login(String account, String password,String key,String value) {
        Result result = null;
        Integer validateCodeRes=validateCodeService.check(key, value);

        if(validateCodeRes==-1){
            result=Result.error("验证码失效！");
            result.put("code",3);
            return result;
        }
        if(validateCodeRes==0){
            result=Result.error("验证码错误！");
            result.put("code",4);
            return result;
        }
        //根据账号查询用户
        QueryWrapper<AuthUser> wrapper=new QueryWrapper<>();
        wrapper.eq("account", account);
        AuthUser user = userMapper.selectOne(wrapper);
        if(ObjectUtils.isEmpty(user)){
            result=Result.error("账号不存在！");
        }
        if(!user.getStatus()){
            result=Result.error("账号被禁用！");
            result.put("code",2);
        }
        if(user.getPassword().equals(password)){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",user.getId());
            map.put("account",account);
            String jwt=jwtUtils.generateToken(map);
            result=Result.ok("登录成功！");
            result.put("auth",jwt);
            result.put("user",user);
            result.put("menus",new ArrayList<>());
            result.put("promiss",new ArrayList<>());
        }else {
            result = Result.error("密码错误！");
            result.put("code",0);
        }
        return result;
    }

    @Override
    public AuthUser courierLogin(String phone,String value) {
        System.out.println("验证码"+value);
        System.out.println("redis键"+key+phone);
        //判断验证码是否失效
        if(!redisTemplate.hasKey(key+phone)){
            System.out.println("未找到redis键");
            return null;
        }
        //从redis获取验证码
        String value1 = redisTemplate.opsForValue().get(key + phone);
        //判断验证码是否正确
        if(value1.equals(value)){
            redisTemplate.delete(key+phone);
            QueryWrapper<AuthUser> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("mobile",phone);
            AuthUser authUser = userMapper.selectOne(queryWrapper);
            CoreOrg coreOrg = coreOrgMapper.selectById(authUser.getOrgId());
            authUser.setCoreOrg(coreOrg);
            return authUser;
        }
        System.out.println("验证码不正确");
        return null;
    }
}
